//
//  ChoiceView.m
//  Food choices
//
//  Created by xalo on 16/3/2.
//  Copyright © 2016年 张广斌. All rights reserved.
//

#import "ChoiceView.h"

@interface ChoiceView ()
@property (nonatomic,assign) BOOL  needRightTable;

//当前的行政区model
//@property (nonatomic,strong) DistrictModel *currentDisModel;
@end

//BOOL _needRightTable = NO;
static NSIndexPath *areaIndex = 0;
static NSIndexPath *bizIndex = 0;
static NSIndexPath *sortIndex = 0;
static NSIndexPath *filterIndex = 0;

@implementation ChoiceView


-(instancetype)initWithFrame:(CGRect)frame needRightTable:(BOOL)isNeed modelArray:(NSArray *)modelArray
{
    
    self = [super initWithFrame:frame];
    if (self) {
        self.modelArray = modelArray;
        
        _needRightTable = isNeed;
        if (_needRightTable) {
            self.leftTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(frame)/3, CGRectGetHeight(frame)/3) style:(UITableViewStylePlain)];
            self.leftTable.dataSource = self;
            self.leftTable.delegate = self;
            self.leftTable.tag = 201;
            [self addSubview:self.leftTable];
            
            self.rightTable = [[UITableView alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.leftTable.frame), 0, CGRectGetWidth(frame)-CGRectGetWidth(self.leftTable.frame), CGRectGetHeight(frame)/3) style:(UITableViewStylePlain)];
            self.rightTable.dataSource = self;
            self.rightTable.delegate = self;
            self.rightTable.tag = 202;

            [self addSubview:self.rightTable];
        }else
        {
            self.leftTable = [[UITableView alloc] initWithFrame:(CGRectMake(0, 0, CGRectGetWidth(frame), CGRectGetHeight(frame)/3)) style:(UITableViewStylePlain)];
            self.leftTable.dataSource = self;
            self.leftTable.delegate = self;
            self.leftTable.tag = 201;
            [self addSubview:self.leftTable];
        }
        self.lucentView = [[UIView alloc] initWithFrame:(CGRectMake(0, self.leftTable.frame.size.height, CGRectGetWidth(frame), 1000))];
        self.lucentView.alpha = 0.5;
        self.lucentView.backgroundColor = [UIColor grayColor];
        [self addSubview:self.lucentView];
    }
    return self;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    if (_needRightTable) {
        if (tableView.tag == 201) {
            return self.modelArray.count;
        }else if (tableView.tag == 202) {
            return self.rightArray.count;
        }else
            return 0;
    }else
    return self.modelArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell"];
    }
    if (_needRightTable) {
        if (tableView.tag == 202) {
//            Biz_areaModel * model = self.rightArray[indexPath.row];
//            cell.textLabel.text = model.biz_area_name;
            cell.textLabel.font = [UIFont systemFontOfSize:16];
        }else if (tableView.tag == 201)
        {
//            DistrictModel * model = self.modelArray[indexPath.row];
//            cell.textLabel.text = model.district_name;
            cell.textLabel.font = [UIFont systemFontOfSize:14];
        }

    }else
    {
        cell.textLabel.text = self.modelArray[indexPath.row];
        cell.textLabel.font = [UIFont systemFontOfSize:16];
    }
    
        return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_needRightTable) {
        switch (tableView.tag) {
            case 201:
            {
//                DistrictModel * model = self.modelArray[indexPath.row];
                
                //当前选中行政区
//                self.currentDisModel = model;
                
//                self.rightArray = model.biz_areas;
                [self.rightTable reloadData];
            }
                break;
            case 202:
            {
//                Biz_areaModel * model = self.rightArray[indexPath.row];
                if (self.delegate!= nil && [self.delegate respondsToSelector:@selector(choiceViewSendParam:dis:)]) {
                    
                    NSLog(@"商圈选择");
//                    [self.delegate choiceViewSendParam:model dis:self.currentDisModel];
                }
            }
            default:
                break;
        }
    }else
    {
        if (self.delegate != nil && [self.delegate respondsToSelector:@selector(choiceViewSendParam:dis:)]) {
//            [self.delegate choiceViewSendParam:[NSString stringWithFormat:@"%ld",indexPath.row] dis:nil];
        }
    }
}


@end
